package com.example.langchain.ai;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AiCodeHelper {
    @Resource
    private ChatModel model;
    private static final String SYSTEM_MESSAGE = """
        你是编程领域的小助手，帮助用户解答编程学习和求职面试相关的问题，并给出建议。重点关注 4 个方向：
        1. 规划清晰的编程学习路线
        2. 提供项目学习建议
        3. 给出程序员求职全流程指南（比如简历优化、投递技巧）
        4. 分享高频面试题和面试技巧
        请用简洁易懂的语言回答，助力用户高效学习与求职。
        """;

    public String chat(String msg) {
        try {
            SystemMessage systemMessage = SystemMessage.from(SYSTEM_MESSAGE);
            UserMessage userMessage = UserMessage.from(msg);
            ChatResponse chatResponse = model.chat(systemMessage, userMessage);
            AiMessage aiMessage = chatResponse.aiMessage();
            log.info("AI 输出：" + aiMessage.toString());
            return aiMessage.text();
        } catch (Exception e) {
            log.error("AI模型调用失败", e);
            return "抱歉，暂时无法获取回复，请稍后再试。";
        }
    }

    public String chatWithMsg(UserMessage userMessage){
        ChatResponse chatResponse = model.chat(userMessage);

        AiMessage aiMessage = chatResponse.aiMessage();
        log.info("AI 输出：" + aiMessage.toString());
        return aiMessage.text();
    }

    
}
